package com.liukai.bishe.presenter.transactionPresenter.impl;


import com.liukai.bishe.model.API;
import com.liukai.bishe.model.doman.transaction.TransactionMessageList;
import com.liukai.bishe.presenter.transactionPresenter.IFindTransactionByUsernamePresenter;
import com.liukai.bishe.utils.Constants;
import com.liukai.bishe.utils.RetrofitManager;
import com.liukai.bishe.utils.ToastUtil;
import com.liukai.bishe.view.IFindTransactionByUsernameCallBack;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class IFindTransactionByUsernamePresenterImpl implements IFindTransactionByUsernamePresenter {

    private IFindTransactionByUsernameCallBack mIFindTransactionByUsernameCallBack = null;


    @Override
    public void getTransactionByName(String username) {

        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<TransactionMessageList> findTransactionByUserNameTask
                = api.findTransactionByUserName(Constants.getUserInfo().getUsername());
        findTransactionByUserNameTask.enqueue(new Callback<TransactionMessageList>() {
            @Override
            public void onResponse(Call<TransactionMessageList> call, Response<TransactionMessageList> response) {

                ToastUtil.log("ITransactionPresenterImpl",
                        "addTransaction",response.body().getTransactionList().toString());
                if (response.code() == HttpURLConnection.HTTP_OK){
                    TransactionMessageList transactionMessageList = response.body();
                    mIFindTransactionByUsernameCallBack.transactionByName(transactionMessageList);
                }

            }

            @Override
            public void onFailure(Call<TransactionMessageList> call, Throwable t) {

            }
        });


    }

    @Override
    public void registerCallback(IFindTransactionByUsernameCallBack iFindTransactionByUsernameCallBack) {
        this.mIFindTransactionByUsernameCallBack = iFindTransactionByUsernameCallBack;
    }

    @Override
    public void unregisterCallback(IFindTransactionByUsernameCallBack iFindTransactionByUsernameCallBack) {
        mIFindTransactionByUsernameCallBack = null;
    }


}
